package com.sgmw.sales.tool.kong.sign.demo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import com.sgmw.sales.tool.kong.sign.utils.HmacSha256Util;
import com.sgmw.sales.tool.kong.sign.utils.Md5Util;

public class HmacTest {
	public static void main(String[] args) {
        test1();
    }
 
    static void test1(){
        String queryParam = "abc";
        String contentMD5 = Md5Util.md5(queryParam);
        System.out.println("Content-md5:"+contentMD5);
 
        Date d=new Date();
        DateFormat format=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        String hdate = format.format(d);
        System.out.println("X-Date:"+hdate);
 
        StringBuilder stb = new StringBuilder();
        String content = stb.append("X-Date: ").append(hdate).append("\n").append("Content-md5: ").append(contentMD5).toString();
        System.out.println("签名前内容: "+content);
        String secret = "test1234";  //用户userhmac的密钥
        try {
        	sun.misc.BASE64Encoder base64 = new sun.misc.BASE64Encoder();
            String signature2 = base64.encode(HmacSha256Util.signatureReturnBytes(content, secret));
            System.out.println("显示指定编码[推荐]:"+signature2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
